import java.util.concurrent.ExecutionException;

import cs2340.spring13.team3.wheresmystuff.R;
import cs2340.spring13.team3.wheresmystuff.presenters.LoginManager;
import cs2340.spring13.team3.wheresmystuff.views.LoginActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.Button;
import android.widget.EditText;

/**
 * Testing class to test the functionality of the LoginActivity and the LoginManager
 * @author Tim Swihart
 * @version 0.1
 * @since 2013-04-06
 */
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
	private LoginManager mLoginManager;
	private LoginActivity mLoginActivity;
	private EditText mEmailV, mPassV;

	private final String INVALID_EMAIL_ERR = "Invalid email";
	private final String INVALID_PASS_ERR = "Password too short";
	private final String EMPTY_FIELD_ERR = "Field is required";


	public LoginActivityTest() {
	    super("cs2340.spring13.team3.wheresmystuff.views.LoginActivity", LoginActivity.class);
	}

	public LoginActivityTest(Class<LoginActivity> activityClass) {
		super(activityClass);
	}

	public void setUp() throws Exception {
		super.setUp();
		mLoginManager = LoginManager.getInstance();
		setActivityInitialTouchMode(false);

		mLoginActivity = getActivity();
		mEmailV = (EditText) mLoginActivity.findViewById(R.id.emailField);
	    mPassV = (EditText) mLoginActivity.findViewById(R.id.passwordField);

	}

	/**
	 * Check that everything is set up properly
	 */
	public void testPreConditions() {
		assertTrue(mLoginActivity != null);
		assertTrue(mEmailV != null);
		assertTrue(mPassV != null);
		assertTrue(mLoginManager != null);
	}
	
	/**
	* Test the validation of the email field
	*/
	@UiThreadTest
	public void testEmailValidation() {
		String validEmail = "t@t.net";
		String invalidEmail = "a+b@c.com"; // our email 'validation' doesn't actually validate emails according to the RFCs...
		
		// Check that emails are validated 'properly'. Our validation isn't actually correct, but I won't get
		// into that...
		assertTrue(mLoginActivity.checkInvalidEmail(validEmail) == false);
		assertTrue(mEmailV.getError() == null);
		
		assertTrue(mLoginActivity.checkInvalidEmail(invalidEmail));
		assertTrue(mEmailV.getError() == INVALID_EMAIL_ERR);
		  
		assertTrue(mLoginActivity.checkInvalidEmail("") == true);
		assertTrue(mEmailV.getError() == EMPTY_FIELD_ERR);
	}

	/**
	* Test the validation of the password field
	*/
	@UiThreadTest
	public void testPasswordValidation() {
		String validPass = "Iamlongerthan4characters";
		String invalidPass = "nop";
		
		// Make sure password is 'valid' (length > 3 chars...)
		assertTrue(mLoginActivity.checkInvalidPassword(validPass) == false);
		assertTrue(mPassV.getError() == null);
		
		assertTrue(mLoginActivity.checkInvalidPassword(invalidPass));
		assertTrue(mPassV.getError() == INVALID_PASS_ERR);
		  
		assertTrue(mLoginActivity.checkInvalidPassword("") == true);
		assertTrue(mPassV.getError() == EMPTY_FIELD_ERR);
	}

	/**
	 * Test logging in with a known User account
	 */
	public void testUserLogin() {
		final String testEmail = "t@t.net";
		final String testPass = "password";
		try {
		runTestOnUiThread(new Runnable() {
			public void run() {
				mEmailV.setText(testEmail);
				mPassV.setText(testPass);
				Button mLoginButton = (Button) mLoginActivity.findViewById(R.id.loginButton);
				mLoginButton.performClick();
				}
			});
		} catch (Throwable e) {
			e.printStackTrace();
		}		
		try {
			// wait for the task to complete
			mLoginActivity.getAuthTask().get(); 
			
			// Make sure we have no error message
			assertTrue(mLoginManager != null);
			assertTrue(mLoginManager.getError() == null || mLoginManager.getError().length() == 0);
			
			// Check to make sure we logged in properly
			assertTrue(mLoginManager.getUser().getEmail().equals(testEmail));
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
}
